﻿using System.IO;
using System.Text;

namespace VS2012_MSHA_Helper
{
    static class MshaBuilder
    {
        #region 私有字段声明

        #endregion

        #region 私有方法

        #endregion

        #region 构造函数

        #endregion

        #region 公共方法
        public static void Build(MshaBookGroup group)
        {
            if (group == null) return;
            if (SaveFolder == null) return;
            var mshaFile = new StringBuilder();
            foreach (MshaBook book in group)
            {
                mshaFile.Clear();
                mshaFile.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");//" +  + "
                mshaFile.Append("    <head />");
                mshaFile.Append("    <body class='vendor-book'>");
                mshaFile.Append("        <div class='details'>");
                mshaFile.Append("            <span class='vendor'>" + group.Vendor + "</span>");
                mshaFile.Append("            <span class='locale'>" + book.Locale + "</span>");
                mshaFile.Append("            <span class='id'>" + book.Id + "</span>");
                mshaFile.Append("            <span class='product'>" + book.Name + "</span>");
                mshaFile.Append("            <span class='name'>" + book.Name + "</span>");
                mshaFile.Append("        </div>");
                mshaFile.Append("        <div class='package-list'>");
                foreach (MshaPackage package in book)
                {
                    mshaFile.Append("            <div class='package'>");
                    mshaFile.Append("                <span class='name'>" + package.Name + "</span>");
                    mshaFile.Append("                <span class='deployed'>" + package.Deployed + "</span>");
                    mshaFile.Append("                <span class='last-modified'>" + package.LastModified + "</span>");
                    mshaFile.Append("                <span class='package-etag'>" + package.Etag + "</span>");
                    var cabFile = Path.GetFileName(package.CurrentLink);
                    mshaFile.Append("                <a class='current-link' href='" + cabFile + "'>" + cabFile + "</a>");
                    mshaFile.Append("            </div>");                    
                }
                mshaFile.Append("        </div>");
                mshaFile.Append("    </body>");
                mshaFile.Append("</html>");
                var data = mshaFile.ToString().Replace("'", "\"");
                var fileName = book.Id + "-" + book.Name + ".msha";
                fileName = SaveFolder + "\\" + fileName;
                using (var stream = new FileStream(fileName, FileMode.OpenOrCreate))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(data);
                        writer.Flush();
                        writer.Close();
                    }   
                    stream.Close();
                }
            }

        }
        #endregion

        #region 属性

        public static string SaveFolder { get; set; }
        #endregion
    }
}
